package com.fortune.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;

import net.sf.json.JSONException;

public class QmailUtil {
	// 短信应用SDK AppID
	// 1400开头
	private static String appid = null; 
	// 短信应用SDK AppKey
	private static String appkey = null;
	// 短信模板ID，需要在短信应用中申请
	// NOTE: 这里的模板ID`7839`只是一个示例，真实的模板ID需要在短信控制台中申请
	private static String templateId = null; 
	// 签名
	// NOTE: 这里的签名"腾讯云"只是一个示例，真实的签名需要在短信控制台中申请，另外签名参数使用的是`签名内容`，而不是`签名ID`
	private static String smsSign = null; 
	static{
		InputStream inputStream = QmailUtil.class.getClassLoader().getResourceAsStream("properties/smail.properties");
		Properties properties = new Properties();
		try {
			//FileInputStream fStream = new FileInputStream("app/smail.properties");
			//properties.load(fStream);
			properties.load(new InputStreamReader(inputStream, "utf-8"));
			appid = properties.getProperty("qs.appid");
			appkey = properties.getProperty("qs.appkey");
			templateId = properties.getProperty("qs.templateId");
			smsSign = properties.getProperty("qs.smsSign");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public final static boolean send(String phone,String code){
		try {
		    SmsSingleSender ssender = new SmsSingleSender(Integer.parseInt(appid), appkey);
		    SmsSingleSenderResult result = ssender.send(0, "86", phone,
		        "【腾讯云】您的验证码是: 5678", "", "");
		    System.out.print(result);
		} catch (HTTPException e) {
		    // HTTP响应码错误
		    e.printStackTrace();
		} catch (JSONException e) {
		    // json解析错误
		    e.printStackTrace();
		} catch (IOException e) {
		    // 网络IO错误
		    e.printStackTrace();
		}		
		return true;
	}
	public final static boolean sendModel(String[] phone,String code,int templateId){
		try {
		    String[] params = {"5678"};
		    SmsSingleSender ssender = new SmsSingleSender(Integer.parseInt(appid), appkey);
		    SmsSingleSenderResult result = ssender.sendWithParam("86", phone[0],
		        templateId, params, smsSign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
		    System.out.print(result);
		} catch (HTTPException e) {
		    // HTTP响应码错误
		    e.printStackTrace();
		} catch (JSONException e) {
		    // json解析错误
		    e.printStackTrace();
		} catch (IOException e) {
		    // 网络IO错误
		    e.printStackTrace();
		}
		return false;
	}
	public static void main(String[] args){
		QmailUtil.send("15620079004", null);
	}
}
